import java.util.Random;

public class Combat {
	private Trainer trainerA;
	private Trainer trainerB;
	
	public Combat(Trainer t1, Trainer t2){
		trainerA = t1;
		trainerB = t2;
	}
	
	public Trainer fight(){
		Pokemon pokemonA = trainerA.throwPokeball();
		Pokemon pokemonB = trainerB.throwPokeball();
		Random random = new Random();
		
		while(pokemonA != null && pokemonB != null){
			if(random.nextInt(2) = 1){
				pokemonB.defend(pokemonA.attack);
				if(pokemonB.isAlive()){
					pokemonA.defend(pokemonB.attack);
				}
			}else{
				pokemonA.defend(pokemonB.attack);
				if(pokemonA.isAlive()){
					pokemonB.defend(pokemonA.attack);
				}
			}
			
			if(pokemonA.isAlive() == false){
				pokemonA = trainerA.throwPokeball();
			}
			if(pokemonB.isAlive() == false){
				pokemonB = trainerB.throwPokeball(); 
			}
		}
		
		// returns winner
		if(pokemonA == null){
			return trainerB;
		}else{
			return trainerA;
		}
	}
}
